import model from '@/utils/model';

export const Classify = (json) => {
  return model(json)
    .string(['categoryName', 'categoryId'])
    .array(['child'])
    .compose((_json) => ({
      isChild: _json.child.length
    }))
    .toJSON();
};

/**
 * @desc 处理角色列表分页
 */
function filterChild(_json) {
  return _json.map((item) => {
    if (!item.child.length) {
      item.child = null;
    } else {
      filterChild(item.child);
    }
    return item;
  });
}

export const filterTreeData = (json) => {
  return model(json)
    .compose((_json) => {
      filterChild(_json);
      return _json;
    })
    .toJSON();
};
